﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Business_Objects;
using ServicesComponent.Repositories;

public partial class QuanLyNhanVien_PhanCongGiangDayDetails : System.Web.UI.Page
{
    private IPhanCongGiangDayRepository _PhanCongGiangDayRepository; 
    private INhanVienRepository _NhanVienRepository; 
    private IClassRepository _classRepository;
    private static string id = "";
    public int Stt;
    private static string action;
    protected void Page_Load(object sender, EventArgs e)
    {
        Stt = Stt + 1;
        _PhanCongGiangDayRepository = FactoryServices.GetServicesPhanCongGiangDay(_PhanCongGiangDayRepository);
        action = Request.QueryString["ac"].ToString();
        id = Request.QueryString["id"].ToString();
        if (IsPostBack == false)
        {

            //if (action == "'sua'")
            //{
            //    GetDataToComboboxEdit();
            //    PhanCongGiangDay obj = _PhanCongGiangDayRepository.Get(id);
            //    ddlGiaoVien.SelectedValue = obj.NhanVienID.ToString();
            //    // ddlKhoi.SelectedValue = id;
            //    txtNgayVao.Text = obj.StartDate.ToShortDateString();
            //    txtNgayRa.Text = obj.Endate.ToShortDateString();
            //    ddlKhoi.Enabled = false;
            //    txtNgayVao.Attributes.Add("readonly", "readonly");
            //    txtNgayRa.Attributes.Add("readonly", "readonly");
            //    txtNgayVao.Enabled = false;
            //    txtNgayRa.Enabled = false;
            //    GetDataRepeatEdit(ddlKhoi.SelectedValue);

            //}
            if (action == "'them'")
            {
                //   lblTitle.Text = "Thêm mới niên khóa";
                GetDataToComboboxAdd();
                GetDataRepeat();
            }
        }
    }
    private void GetDataToComboboxAdd()
    {
        _NhanVienRepository = FactoryServices.GetServicesNhanVien(_NhanVienRepository);
        ddlGiaoVien.DataSource =
            _NhanVienRepository.GetGiaoVienBySchoolIdAndSchoolYearIDNotPhanLop(int.Parse(Session["SchoolID"].ToString()));
        ddlGiaoVien.DataValueField = "Id";
        ddlGiaoVien.DataTextField = "HoTen";
        ddlGiaoVien.DataBind();


    }
    private void GetDataToComboboxEdit()
    {
        _NhanVienRepository = FactoryServices.GetServicesNhanVien(_NhanVienRepository);
        ddlGiaoVien.DataSource = _NhanVienRepository.GetList();
        ddlGiaoVien.DataValueField = "Id";
        ddlGiaoVien.DataTextField = "HoTen";
        ddlGiaoVien.DataBind();
    }
    protected void rpt_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {

        //if (action == "'sua'")
        //{
        //    //Khó có thẻ list source 1 trable - details 1 table
        //    foreach (RepeaterItem item in rptA.Items)
        //    {
        //        HiddenField hi = item.FindControl("HdField") as HiddenField;
        //        if (hi != null)
        //        {
        //            DiemDanh obj = _PhanCongGiangDayRepository.GetDiemDanhBySchoolIdAndSchoolYearAndStudentId(Session["SchoolYearID"].ToString(),
        //                                                                                              int.Parse(Session["SchoolID"].ToString()), hi.Value);
        //            CheckBox ch = item.FindControl("ChBox") as CheckBox;
        //            ch.Checked = obj.Comat;
        //        }
        //    }
        //}
        //else
        //{
        //    RepeaterItem ri = e.Item;
        //    var isDefaultCheckBox = ri.FindControl("ChBox") as CheckBox;
        //    if (isDefaultCheckBox != null) isDefaultCheckBox.Checked = true;
        //}

    }
    //Get edit
    private void GetDataRepeatEdit(string classId)
    {
        _classRepository = FactoryServices.GetServicesClass(_classRepository);
        List<Class> list = _classRepository.GetClassBySchoolIdAndSchoolYearIDNotPhanLop(int.Parse(Session["SchoolID"].ToString()), Session["SchoolYearID"].ToString());
        //_StudentRepository = FactoryServices.GetServicesStudent(_StudentRepository);
        //List<Student> list1 = _StudentRepository.GetStudentBySchoolIdAndSchoolYearClass(Session["SchoolYearID"].ToString(),
        //                                                               int.Parse(Session["SchoolID"].ToString()), classId);

        //foreach (var student in list1)
        //{
        //    student.Unrefuge = true;
        //}
        //List<Student> list = _StudentRepository.GetStudentBySchoolIdAndSchoolYearNotClass(Session["SchoolYearID"].ToString(),
        //                                                             int.Parse(Session["SchoolID"].ToString()));
        //foreach (var student in list)
        //{
        //    student.Unrefuge = false;
        //    list1.Add(student);
        //}
        rptA.DataSource = list;
        rptA.DataBind();

        Cpage.DataSource = list;
        Cpage.BindToControl = rptA;
        rptA.DataSource = Cpage.DataSourcePaged;
    }
    //get add - ClassID luôn là duy nhất
    void GetDataRepeat()
    {
        _classRepository = FactoryServices.GetServicesClass(_classRepository);
        List<Class> list = _classRepository.GetClassBySchoolIdAndSchoolYearIDNotPhanLop(int.Parse(Session["SchoolID"].ToString()), Session["SchoolYearID"].ToString());
        rptA.DataSource = list;
        rptA.DataBind();
        Cpage.DataSource = list;
        Cpage.BindToControl = rptA;
        rptA.DataSource = Cpage.DataSourcePaged;
    }
    protected void ddlKhoi_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlGiaoVien.SelectedIndex == 0)
        {
            //  bind();
        }
        else
        {
            //   bind2();
        }
    }
    bool ValiDate()
    {
       
        return true;
    }
    protected void btnUpdate_Click(object sender, EventArgs e)
    {

        if (ValiDate())
        {

            if (action == "'sua'")
            {
                //foreach (RepeaterItem item in rptA.Items)
                //{
                //    CheckBox ch = item.FindControl("ChBox") as CheckBox;
                //    HiddenField oField = item.FindControl("HdField") as HiddenField;
                //    if (!ch.Checked)
                //    { //Edit
                //        //  Guid idStu = Guid.Parse(oField.Value.ToString());
                //        if (oField != null) _PhanLopRepository.DeleteStudentFromPhanLop(oField.Value);
                //        // _PhanLopRepository.Delete(new PhanLop(Guid.Parse(id), Guid.Parse(ddlKhoi.SelectedValue.ToString()), idStu, Guid.Parse(Session["SchoolYearID"].ToString()), Convert.ToDateTime(txtNgayVao.Value.ToString()), Convert.ToDateTime(txtNgayRa.Value.ToString())));
                //        //// ch.Text = "IT is selected now";
                //    }
                //}

                //Alert.Show("Cập nhật dữ liệu thành công!");
                //Alert.ClodePopup();


            }
            if (action == "'them'")
            {
                foreach (RepeaterItem item in rptA.Items)
                {
                    CheckBox ch = item.FindControl("ChBox") as CheckBox;
                    HiddenField oField = item.FindControl("HdField") as HiddenField;
                    if (ch.Checked)
                    { //AddNew
                        Guid idClass = Guid.Parse(oField.Value.ToString());
                        _PhanCongGiangDayRepository.Insert(new PhanCongGiangDay(Guid.NewGuid(), idClass,Guid.Parse(Session["SchoolYearID"].ToString()),Guid.Parse(ddlGiaoVien.SelectedValue.ToString()),true));
                    }
                }

                Alert.Show("Thêm mới dữ liệu thành công!");
                Alert.ClodePopup();
            }
        }
        else
        {
            Alert.Show("Thông tin cần nhập chưa đầy đủ !");
        }

    }
}